package com.eway.viewanimator;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Value_Animator_Activity extends AppCompatActivity implements View.OnClickListener {
	private static String TAG = Value_Animator_Activity.class.getSimpleName();

	private Button valueAnimatorStart, cancelValueAnimator, removeListener, value_loading;
	private TextView showValueAnimator;
	private ValueAnimator valueAnimator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_value__animator);

		valueAnimatorStart = (Button) findViewById(R.id.value_animator_start);
		showValueAnimator = (TextView) findViewById(R.id.show_value_animator);
		cancelValueAnimator = findViewById(R.id.cancle_animator);
		removeListener = findViewById(R.id.remove_listener);
		value_loading = findViewById(R.id.value_loading);


		valueAnimatorStart.setOnClickListener(this);
		showValueAnimator.setOnClickListener(this);
		cancelValueAnimator.setOnClickListener(this);
		removeListener.setOnClickListener(this);
		value_loading.setOnClickListener(this);


	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.value_animator_start:
				valueAnimator = ValueAnimator.ofInt(0, 400, 600);
				valueAnimator.setDuration(3000);

				valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
					@Override
					public void onAnimationUpdate(ValueAnimator animation) {
						int animteVale = (int) animation.getAnimatedValue();
						Log.d(TAG, "value_animator-----" + animteVale);
						showValueAnimator.layout(animteVale, animteVale, animteVale + showValueAnimator.getWidth(), animteVale + showValueAnimator.getHeight());
					}
				});


				valueAnimator.addListener(new Animator.AnimatorListener() {
					@Override
					public void onAnimationStart(Animator animation) {
						Log.d("zwk", "onAnimationStart");
					}

					@Override
					public void onAnimationEnd(Animator animation) {
						Log.d("zwk", "onAnimationEnd");

					}

					@Override
					public void onAnimationCancel(Animator animation) {
						Log.d("zwk", "onAnimationCancel");
					}

					@Override
					public void onAnimationRepeat(Animator animation) {
						Log.d("zwk", "onAnimationRepeat");

					}
				});
				valueAnimator.setDuration(1000);
				valueAnimator.setRepeatMode(ValueAnimator.REVERSE); // 倒叙
				valueAnimator.setRepeatCount(ValueAnimator.INFINITE); // 无线循环
				valueAnimator.start();

				break;

			case R.id.show_value_animator:
				Toast.makeText(Value_Animator_Activity.this, "属性动画移动后，新位置具有点击事件", Toast.LENGTH_SHORT).show();
				break;

			case R.id.cancle_animator:

				if (valueAnimator != null && valueAnimator.isRunning()) {
					valueAnimator.cancel();
				}
				break;
			case R.id.remove_listener:
				if (valueAnimator != null) {
					valueAnimator.removeAllListeners();
				}
				break;

			case R.id.value_loading:
				IntentUtils.intentUtil(Value_Animator_Activity.this, LoadingActivity.class);
				break;

			default:
				break;
		}
	}

}
